import md5 from 'js-md5';
import {

	assign
} from 'lodash';
import {
	useStore
} from "@/store/index";
export default function adapter(config) {
	// console.log("config=" + JSON.stringify(config))
	let option = {
		requestBody: {
			data: null,
			timestamp: null
		},

	}

	let timestamp = new Date().getTime();
	config.data = config.data || {};
	let sign = JSON.stringify(config.data) + timestamp;
	for (let i = 0; i < 25; i++) {
		sign = md5(sign);
	}
	config.data = assign(option.requestBody, {
		data: JSON.stringify(config.data),
		timestamp: timestamp
	});
	const store = useStore()
	const token = store.tokenInfo?.token


	let appInfo = {
		"sign": sign,
		"appName": "银迅伙伴商户端",
		"name": "yxhb",
		"platform": "H5",
		"jPushDeviceId": "160a3797c90a8f90e4d",
		"loginToken": token,
		"appNo": "1000",
		"appVersion": "1.0",
		"systemVersion": "1.0"
	}

	return new Promise((resolve, reject) => {
		if (config.showLoading) {
			uni.showLoading({
				title: '加载中',
				mask: true
			})
		}


		let requestTask = uni.request({

			url: config.url,
			header: {
				'app-info': encodeURI(JSON.stringify(appInfo), "UTF-8")
			},
			...config,
			complete(response) {
				// console.log("response=" + JSON.stringify(response));
				// console.log("config=" + JSON.stringify(config));
				const result = Object.assign({}, {
					config
				}, response);
				// console.log("result=" + JSON.stringify(result));
				if (response.data.code == 200) {
					resolve(result)
				} else {
					reject(result)
				}
				// (!config.validateStatus || config.validateStatus(response.statusCode)) ?
				// resolve(result): reject(result)
				if (config.showLoading) {
					uni.hideLoading()
				}
			}
		})
		config.fetcher?.resolve(requestTask)
	})
}